package com.mathlearning.auth.controller;

import com.mathlearning.auth.common.ApiResponse;
import com.mathlearning.auth.dto.problem.*;
import com.mathlearning.auth.service.ProblemAdminService;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 题目管理接口（管理员专用）
 */
@RestController
@RequestMapping("/api/admin/problems")
public class ProblemAdminController {

    private static final Logger logger = LoggerFactory.getLogger(ProblemAdminController.class);

    @Autowired
    private ProblemAdminService problemAdminService;

    /**
     * 创建题目
     */
    @PostMapping
    public ApiResponse<ProblemAdminResponse> createProblem(
            @Valid @RequestBody CreateProblemRequest request,
            @RequestAttribute Long userId) {
        logger.info("管理员 {} 创建题目: {}", userId, request.getTitle());
        ProblemAdminResponse response = problemAdminService.createProblem(request);
        return ApiResponse.success("题目创建成功", response);
    }

    /**
     * 更新题目
     */
    @PutMapping("/{problemId}")
    public ApiResponse<ProblemAdminResponse> updateProblem(
            @PathVariable Long problemId,
            @RequestBody UpdateProblemRequest request,
            @RequestAttribute Long userId) {
        logger.info("管理员 {} 更新题目: {}", userId, problemId);
        ProblemAdminResponse response = problemAdminService.updateProblem(problemId, request);
        return ApiResponse.success("题目更新成功", response);
    }

    /**
     * 删除题目
     */
    @DeleteMapping("/{problemId}")
    public ApiResponse<Void> deleteProblem(
            @PathVariable Long problemId,
            @RequestAttribute Long userId) {
        logger.info("管理员 {} 删除题目: {}", userId, problemId);
        problemAdminService.deleteProblem(problemId);
        return ApiResponse.success("题目删除成功");
    }

    /**
     * 获取题目编辑信息（含答案和题解）
     */
    @GetMapping("/{problemId}")
    public ApiResponse<ProblemEditResponse> getProblemForEdit(
            @PathVariable Long problemId,
            @RequestAttribute Long userId) {
        logger.info("管理员 {} 获取题目编辑信息: {}", userId, problemId);
        ProblemEditResponse response = problemAdminService.getProblemForEdit(problemId);
        return ApiResponse.success(response);
    }

    /**
     * 批量导入题目
     */
    @PostMapping("/batch")
    public ApiResponse<BatchImportResponse> batchImport(
            @Valid @RequestBody BatchImportRequest request,
            @RequestAttribute Long userId) {
        logger.info("管理员 {} 批量导入题目，数量: {}", userId, request.getProblems().size());
        BatchImportResponse response = problemAdminService.batchImport(request);
        return ApiResponse.success("批量导入完成", response);
    }
}



